<!-- 定时任务管理 -->
<template>
  <div class="" :style="styleObj">
    <ux-grid
      class="tableListValue"
      :key="random"
      :data="TableList"
      ref="plxTable"
      stripe
      :border="false"
      :max-height="styleObj.tableHeightCopy"
      size="mini"
      :header-cell-style="{ 'text-align': 'center' }"
      style="width: 100%; font-size: 12px"
      :highlight-current-row="true"
      show-overflow
    >
      <ux-table-column
        v-for="(item, index) in columns"
        :key="index"
        :title="item.label"
        :field="item.prop"
        :align="item.align"
        :width="item.width"
        resizeable
      >
      </ux-table-column>
    </ux-grid>
  </div>
</template>

<script>
import { myStorage } from "@/utils/localStorage";
import YgTable from "@/components/Table/YgTable.vue";
import YgFrom from "@/components/Form/YgFrom.vue";
import { onMsgExt } from "@/mqtt/msg.js";
import { publishMsg, setMessageCallback, removeMessageCallback } from "@/mqtt";
import { tableMixins } from "./mixin/index";
export default {
  name: "widgetTaskData",
  mixins: [tableMixins],
  components: {
    YgTable,
    YgFrom,
  },
  props: {
    value: Object,
    ispreview: Boolean,
  },
  data() {
    //这里存放数据
    return {
      TableList: [],
      columns: [
        {
          prop: "id",
          label: "数据id",
          align: "left",
          width: "150",
        },
        {
          prop: "name",
          label: "名称",
          align: "left",
        },
        {
          prop: "desc",
          label: "描述",
        },
        {
          prop: "value",
          label: "值",
        },
        {
          prop: "unit",
          label: "单位",
          width: "50",
        },
        {
          prop: "update_time",
          label: "刷新时间",
          width: "160",
        },
      ],
    };
  },
  methods: {
    // MQTT 监听消息
    onMessage(topic, msg) {
      switch (msg.webMsgType) {
        case 1015:
          switch (msg.funCode) {
            case 0:
              if (msg.retCode != 0 || !msg.data) {
                return this.$message.error(msg.retMsg);
              }
              this.TableList = msg.data;
              break;
            case 1:
              if (msg.retCode != 0) {
                return this.$message.error(msg.retMsg);
              } else if (msg.retCode == 0) {
                this.$message.success(msg.retMsg);
              }
              break;
            case 2:
              if (msg.retCode != 0) {
                return this.$message.error(msg.retMsg);
              } else {
                return this.$message.success(msg.retMsg);
              }
              break;
            case 3:
              if (msg.retCode != 0) {
                return this.$message.error(msg.retMsg);
              }
              break;
            case 4:
              if (msg.retCode != 0 || !msg.data) {
                return this.$message.error(msg.retMsg);
              }
              for (let i = 0; i < this.TableList.length; i++) {
                if (this.TableList[i].id == msg.data.id) {
                  this.TableList[i].value = msg.data.value;
                  this.TableList[i].update_time = msg.data.update_time;
                }
              }
              break;
            default:
              break;
          }
          break;
        default:
          break;
      }
    },
  },

  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    //this.Onmessage();
    this.options = this.value;
    setMessageCallback(this.onMessage);
    publishMsg({
      webMsgType: 1015,
      funCode: 0,
    });
  },
  destroyed() {
    removeMessageCallback(this.onMessage);
  },
};
</script>
<style lang="scss" scoped>
@import "./css/index.scss";
</style>
